﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MyMovie
{
    public partial class Form1 : Form
    {
        #region Aero Glass
        [StructLayout(LayoutKind.Sequential)]
        public class Margins
        {
            public int Left, Right,
                       Top, Bottom;

            public Margins(int left, int top, int right, int bottom)
            {
                Left = left; Top = top;
                Right = right; Bottom = bottom;
            }
        }

        #region DllImport's
        [DllImport("dwmapi.dll")]
        static extern void DwmIsCompositionEnabled(ref bool pfEnabled);
        [DllImport("dwmapi.dll")]
        static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMargins);
        #endregion

        public bool isGlassSupported()
        {
            bool isGlassSupported = false;
            DwmIsCompositionEnabled(ref isGlassSupported);
            return isGlassSupported;
        }

        // make windows do the work for us by lieing to it about where the user clicked
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == 0x84 // if this is a click
                && m.Result.ToInt32() == 1 // ...and it is on the client
                && this.IsOnGlass(m.LParam.ToInt32())) // ...and specifically in the glass area
            {
                m.Result = new IntPtr(2); // lie and say they clicked on the title bar
            }
        }

        private bool IsOnGlass(int lParam)
        {
            // get screen coordinates
            int x = (lParam << 16) >> 16; // lo order word
            int y = lParam >> 16; // hi order word

            // translate screen coordinates to client area
            Point p = this.PointToClient(new Point(x, y));

            // work out if point clicked is on glass
            //if (y < marg.Top)
                return true;

            return false;
        }

        Margins marg;

        public void Extend()
        {
            marg = new Margins(0, 0, 200, 0);
            DwmExtendFrameIntoClientArea(this.Handle, ref marg);
        } 
        #endregion

        public Form1()
        {
            Extend();
            InitializeComponent();
            Bitmap bmp = new Bitmap(1, 1);
            bmp.SetPixel(0, 0, Color.Transparent);
            //Icon = Icon.FromHandle(bmp.GetHicon());            
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);

            using (Graphics g = e.Graphics)
            {
                SolidBrush blackBrush = new SolidBrush(Color.Black);
                g.FillRectangle(blackBrush, 0, 0, this.ClientSize.Width, marg.Top);
            }
        }
    }
}
